package com.cp.admin.scheduled;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.cp.admin.modular.user.model.ChatgroupApply;
import com.cp.admin.modular.user.model.UserFriendApply;
import com.cp.admin.modular.user.model.Vip;
import com.cp.admin.modular.user.service.IChatgroupApplyService;
import com.cp.admin.modular.user.service.IUserFriendApplyService;
import com.cp.admin.modular.user.service.IVipService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;



@Component
@Slf4j
public class ScheduledTimes {
    @Autowired
    private IVipService vipService;
    @Autowired
    private IChatgroupApplyService chatgroupApplyService;
    @Autowired
    private IUserFriendApplyService userFriendApplyService;

/**
     * 定时任务 每天vip时限-1 ，领取豌豆状态变为0
     **/

    @Scheduled(cron = "0 0 01 * * ?")
    public void vipping() {
        List<Vip> vip = vipService.selectList(new EntityWrapper().eq("status", 1));
        for (Vip newvip : vip) {
            if (newvip.getRemainingDays() >= 1) {
                newvip.setRemainingDays(newvip.getRemainingDays() - 1);
                newvip.setGetPeasStatus(0);
                vipService.updateById(newvip);
            }else {
                newvip.setRemainingDays(0);
                newvip.setStatus(-1);
                newvip.setUpdateTime(new Date());
                newvip.setGetPeasStatus(0);
                if (vipService.updateById(newvip)){
                    log.info("--------用户"+newvip.getUserId()+"的vip已到期-------");
                }
            }

        }
        log.info("------------所有vip剩余天数-1------------");
    }


    /**
     * 定时任务 每2秒查询好友申请列表是否过期
     **/
    @Scheduled(cron = "0/2 * * * * ? ")
    public void friendApplyOverdue() {
        Wrapper wrapper=new EntityWrapper();
        wrapper.le("overdue_time",new Date());
        wrapper.eq("status",0);
       List<UserFriendApply> result=userFriendApplyService.selectList(wrapper);
       for (UserFriendApply applys:result){
           applys.setStatus(-1);
           userFriendApplyService.updateById(applys);
       }
    }

    /**
     * 定时任务 每2秒查询加入群聊申请列表是否过期
     **/
    @Scheduled(cron = "0/2 * * * * ? ")
    public void chatgroupApplyOverdue() {
        Wrapper wrapper=new EntityWrapper();
        wrapper.le("overdue_time",new Date());
        wrapper.eq("status",0);
        List<ChatgroupApply> result=chatgroupApplyService.selectList(wrapper);
        for (ChatgroupApply applys:result){
            applys.setStatus(-1);
            chatgroupApplyService.updateById(applys);
        }
    }


}
